package com.cedar.demo.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cedar.demo.common.Result;
import com.cedar.demo.demo.Client;
import com.cedar.demo.demo.MeData;
import com.cedar.demo.entity.ConnectionData;
import com.cedar.demo.entity.CorbaLoginReq;
import com.cedar.demo.entity.CorbaLoginRes;
import com.cedar.demo.service.HuaweiNceCorbaService;
import com.cedar.demo.utils.JsonUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mtnm.tmforum.org.emsMgr.AlarmStaticInfo_T;
import mtnm.tmforum.org.emsSession.EmsSession_I;
import mtnm.tmforum.org.equipment.EquipmentOrHolder_T;
import mtnm.tmforum.org.equipment.ObjectAdditionalInfo_T;
import mtnm.tmforum.org.equipment.PhysicalLocationInfo_T;
import mtnm.tmforum.org.globaldefs.NameAndStringValue_T;
import mtnm.tmforum.org.globaldefs.ProcessingFailureException;
import mtnm.tmforum.org.managedElement.ManagedElement_T;
import mtnm.tmforum.org.multiLayerSubnetwork.MultiLayerSubnetwork_T;
import mtnm.tmforum.org.performance.CollectTaskInfo_T;
import mtnm.tmforum.org.subnetworkConnection.CrossConnect_T;
import mtnm.tmforum.org.subnetworkConnection.SubnetworkConnection_T;
import mtnm.tmforum.org.terminationPoint.TerminationPoint_T;
import mtnm.tmforum.org.topologicalLink.TopologicalLink_T;

@RestController
@Api(tags = "API-2华为NCE_CORBA采集测试")
@RequestMapping("/test/corba")
public class TestCorbaController {
	
	@Autowired
	private HuaweiNceCorbaService huaweiNceCorbaService;
	
	@Autowired
	private CorbaLoginReq corbaLoginReq;
	
	@ApiOperation(value = "1corba获取数据示例")
	@PostMapping("/huawei/nce/demo")
	public Result<String> demo(){
		String[] args = {corbaLoginReq.getHost(),corbaLoginReq.getPort(),corbaLoginReq.getUserName(),corbaLoginReq.getPassWord()};
		Client.main(args);
		return Result.success("");
	}
	
	
	@ApiOperation(value = "2华为NCE登录")
	@PostMapping("/huawei/nce/login")
	public Result<EmsSession_I> huaweiNcelogin(){
		EmsSession_I loginNce = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(loginNce);
	}
	
	@ApiOperation(value = "3获取所有支持的管理")
	@PostMapping("/huawei/nce/supported/managers")
	public Result<List<String>> getAllSupportedManagers() throws ProcessingFailureException {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<String> list = huaweiNceCorbaService.getAllSupportedManagers(login);
		return Result.success(list);
	}
	
	@ApiOperation(value = "323查询NCE系统中所有子网详细信息")
	@PostMapping("/huawei/nce/all/sys")
	public Result<List<MultiLayerSubnetwork_T>> getAllTopLevelSubnetworks() throws InvalidName, AdapterInactive, NotFound, CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName, ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<MultiLayerSubnetwork_T> list = huaweiNceCorbaService.getAllTopLevelSubnetworks(login);
		return Result.success(list);
	}
	
	@ApiOperation(value = "341获取所有网元")
	@PostMapping("/huawei/nce/all/ne")
	public Result<List<ManagedElement_T>> getAllManagedElements()  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<ManagedElement_T> allNe = huaweiNceCorbaService.getAllManagedElements(login);
		return Result.success(allNe);
	}
	@ApiOperation(value = "344getManagedElement")
	@PostMapping("/huawei/nce/getManagedElement")
	public Result<ManagedElement_T> getManagedElement() throws ProcessingFailureException, UnsupportedEncodingException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		ManagedElement_T managedElement = huaweiNceCorbaService.getManagedElement(login, "3145775");
		Result<ManagedElement_T> ok = Result.success(managedElement);
		//ok.setMsg(new String(JsonUtils.javaBeanToString(managedElement).getBytes("iso-8859-1")));
		return ok;
	}
	
	@ApiOperation(value = "3310获取物理信息")
	@PostMapping("/huawei/nce/all/physical")
	public Result<List<PhysicalLocationInfo_T>> getPhysicalLocationInfo() throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<PhysicalLocationInfo_T> list = huaweiNceCorbaService.getPhysicalLocationInfo(login);
		return Result.success(list);
	}
	
	@ApiOperation(value = "332获取指定网元的所有设备")
	@PostMapping("/huawei/nce/all/equipment/{stElement}")
	public Result<List<EquipmentOrHolder_T>> getAllEquipment(@PathVariable String stElement) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getAllEquipment(login,stElement));
	}
	
	/*@ApiOperation(value = "358获取指定拓扑信息")
	@ApiOperationSupport(order = 8)
	@PostMapping("/huawei/nce/all/topological")
	public Result<List<TopologicalLink_T>> getTopologicalLink() throws ProcessingFailureException  {
		return Result.success(huaweiNceCorbaService.getTopologicalLink());
	}*/
	
	@ApiOperation(value = "334getContainedEquipment获取板卡")
	@PostMapping("/huawei/nce/getContainedEquipment/{stElement}/{EquipmentHolder}")
	public Result<List<EquipmentOrHolder_T>> getContainedEquipment(@PathVariable String stElement, @PathVariable String EquipmentHolder) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getContainedEquipment(login,stElement, EquipmentHolder));
	}
	
	@ApiOperation(value = "333getAllEquipmentNames")
	@PostMapping("/huawei/nce/getAllEquipmentNames/{stElement}")
	public Result<NameAndStringValue_T[][] > getAllEquipmentNames(@PathVariable String stElement) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getAllEquipmentNames(login,stElement));
	}
	
	@ApiOperation(value = "335getAllSupportedPTPs")
	@PostMapping("/huawei/nce/getAllSupportedPTPs/{stElement}/{equipmentHolder}/{equipment}")
	public Result<List<TerminationPoint_T>> getAllSupportedPTPs(@PathVariable String stElement, @PathVariable String equipmentHolder, @PathVariable String equipment) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getAllSupportedPTPs(login,stElement,equipmentHolder,equipment));
	}
	
	@ApiOperation(value = "339getEquipmentStaticInfo")
	@PostMapping("/huawei/nce/getEquipmentStaticInfo/{stElement}")
	public Result<NameAndStringValue_T[][]> getEquipmentStaticInfo(@PathVariable String stElement) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getEquipmentStaticInfo(login,stElement));
	}
	@ApiOperation(value = "3310getAllEquipmentAdditionalInfo")
	@PostMapping("/huawei/nce/getAllEquipmentAdditionalInfo/{stElement}")
	public Result<List<ObjectAdditionalInfo_T>> getAllEquipmentAdditionalInfo(@PathVariable String stElement) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getAllEquipmentAdditionalInfo(login,stElement));
	}
	@ApiOperation(value = "3416getAllCrossConnections")
	@PostMapping("/huawei/nce/getAllCrossConnections/{stElement}/{rate}")
	public Result<List<CrossConnect_T>> getAllCrossConnections(@PathVariable String stElement, @PathVariable String rate) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getAllCrossConnections(login,stElement,rate));
	}
	
	@ApiOperation(value = "3511getAllSubnetworkConnections")
	@PostMapping("/huawei/nce/getAllSubnetworkConnections/{subnetName}/{rate}")
	public Result<List<SubnetworkConnection_T>> getAllSubnetworkConnections(@PathVariable String subnetName, @PathVariable String rate) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<SubnetworkConnection_T> allSubnetworkConnections = huaweiNceCorbaService.getAllSubnetworkConnections(login,subnetName,rate);
		List<ConnectionData> list = new ArrayList<>();
		int index = 1;
		for (SubnetworkConnection_T temp : allSubnetworkConnections) {
			ConnectionData data = new ConnectionData();
			try {
				System.out.println("第" + index + "个CrossConnect_T");
				index ++;
				List<CrossConnect_T> route = huaweiNceCorbaService.getRoute(login, temp.name[2].value);
				data.setCrossConnectList(route);
				data.setSubnetworkConnection(temp);
				list.add(data);
			} catch (ProcessingFailureException e) {
				e.printStackTrace();
			}
		}
		
		String str = JsonUtils.javaBeanToString(Result.success(list));
		try {
			JsonUtils.initFileJson("C:\\workspace\\subnetwork\\"+subnetName+".json", new String(str.getBytes("iso-8859-1")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return Result.success(allSubnetworkConnections);
	}
	
	@ApiOperation(value = "354getAllTopologicalLinks")
	@PostMapping("/huawei/nce/getAllTopologicalLinks/{subnetName}")
	public Result<List<TopologicalLink_T>> getAllTopologicalLinks(@PathVariable String subnetName) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		List<TopologicalLink_T> allTopologicalLinks = huaweiNceCorbaService.getAllTopologicalLinks(login,subnetName);
		String str = JsonUtils.javaBeanToString(Result.success(allTopologicalLinks));
		try {
			JsonUtils.initFileJson("C:\\workspace\\topologicalLinks\\"+subnetName+".json", new String(str.getBytes("iso-8859-1")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return Result.success(allTopologicalLinks);
	}
	
	@ApiOperation(value = "3515getgetRoute")
	@PostMapping("/huawei/nce/getRoute")
	public Result<List<CrossConnect_T>> getRoute(String topologicalLink) throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getRoute(login, topologicalLink));
	}
	@ApiOperation(value = "3516getSNC")
	@PostMapping("/huawei/nce/getSNC")
	public Result<SubnetworkConnection_T> getSNC() throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		Result<SubnetworkConnection_T> ok = Result.success(huaweiNceCorbaService.getSNC(login));
		
		// try {
		// 	ok.setMsg(new String(JsonUtils.javaBeanToString(huaweiNceCorbaService.getSNC(login)).getBytes("iso-8859-1")));
		// } catch (UnsupportedEncodingException e) {
		// 	// TODO Auto-generated catch block
		// 	e.printStackTrace();
		// } catch (ProcessingFailureException e) {
		// 	// TODO Auto-generated catch block
		// 	e.printStackTrace();
		// }
		return ok;
	}
	
	@ApiOperation(value = "3521getOrginalRoute")
	@PostMapping("/huawei/nce/getOrginalRoute")
	public Result<List<CrossConnect_T>> getOrginalRoute() throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		return Result.success(huaweiNceCorbaService.getOrginalRoute(login));
	}
	
	@ApiOperation(value = "获取所有网元关联的数据")
	@PostMapping("/huawei/nce/neallrelation")
	public Result<List<MeData>> neAllRelation() throws ProcessingFailureException  {
		huaweiNceCorbaService.neAllRelation();
		return Result.success(huaweiNceCorbaService.neAllRelation());
	}
	
	@ApiOperation(value = "生成告警数据到FTPgetAllHistoryAlarmsByFTP")
	@PostMapping("/huawei/nce/getAllHistoryAlarmsByFTP")
	public Result<String> getAllHistoryAlarmsByFTP() throws ProcessingFailureException  {
		EmsSession_I login = huaweiNceCorbaService.login().getEmsSession();
		huaweiNceCorbaService.getAllHistoryAlarmsByFTP(login);
		return Result.success("ok");
	}
	
	@ApiOperation(value = "获取当前告警数据getAllEMSAndMEActiveAlarms")
	@PostMapping("/huawei/nce/getAllEMSAndMEActiveAlarms")
	public Result<List<Map<String, String>>> getAllEMSAndMEActiveAlarms() throws ProcessingFailureException  {
		CorbaLoginRes login = huaweiNceCorbaService.login();
		List<Map<String, String>> list = huaweiNceCorbaService.getAllEMSAndMEActiveAlarms(login);
		return Result.success(list);
	}
	
	@ApiOperation(value = "获取当前告警静态数据getAllAlarmStaticInfo")
	@PostMapping("/huawei/nce/getAllAlarmStaticInfo")
	public Result<List<AlarmStaticInfo_T>> getAllAlarmStaticInfo() throws ProcessingFailureException  {
		CorbaLoginRes login = huaweiNceCorbaService.login();
		List<AlarmStaticInfo_T> list = huaweiNceCorbaService.getAllAlarmStaticInfo(login);
		list.forEach(temp -> {
			try {
				temp.nativeProbableCause = new String(temp.nativeProbableCause.getBytes("iso-8859-1"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		});
		return Result.success(list);
	}
	
	@ApiOperation(value = "createPMCollectionTask")
	@PostMapping("/huawei/nce/createPMCollectionTask")
	public Result<String> createPMCollectionTask() throws ProcessingFailureException  {
		CorbaLoginRes login = huaweiNceCorbaService.login();
		huaweiNceCorbaService.createPMCollectionTask(login);
		return Result.success("ok");
	}
	
	@ApiOperation(value = "selectPMCollectionTask")
	@PostMapping("/huawei/nce/selectPMCollectionTask")
	public Result<List<CollectTaskInfo_T>> selectPMCollectionTask() throws ProcessingFailureException, UnsupportedEncodingException  {
		CorbaLoginRes login = huaweiNceCorbaService.login();
		List<CollectTaskInfo_T> list = huaweiNceCorbaService.selectPMCollectionTask(login);
		return Result.success(list);
	}
	
	
	
}